<?php
namespace srvs;
use Yii;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;

class BaseService {

    public $model;
    public function __construct() {
        $this->model=$this->getModel();
    }

    public static function getModelName(){
        return explode("_",Inflector::camel2id(StringHelper::basename(get_called_class()), '_'))[0];
    }

    public function getModel(){
        $model_name=static::getModelName();
        if(strpos($model_name,"_")===0){
            $model_name=str_replace("_","",$model_name);
        }
        $comType = '\\app\\models\\' . ucfirst($model_name) . 'Model';
        return new $comType();
    }

    public function getAll($filters=[],$sorts=[],$limit=0,$skip=0){
        return $this->model->getAll($filters,$sorts,$limit,$skip);
    }

    public function getCount($filters=[]){
        return $this->model->getCount($filters);
    }

    public function getOne($filters=[]){
        return $this->model->getOne($filters);
    }

    public function getOneById($object_id){
        return $this->model->getOneById($object_id);
    }

    public function add($data){
        return $this->model->add($data);
    }

    public function update($data){
        return $this->model->update($data);
    }

    public function delete($object_id){
        return $this->model->delete($object_id);
    }
}